package word;

import org.apache.poi.wp.usermodel.HeaderFooterType;
import org.apache.poi.xwpf.model.XWPFHeaderFooterPolicy;
import org.apache.poi.xwpf.usermodel.*;
import org.junit.Test;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTHdrFtr;

import java.io.*;

/**
 * @author weixinxin 2024-01-26
 **/
public class 真word文件添加水印 {
    public static void main(String[] args) throws Exception {
        File file = new File("D:\\Win10Driver\\OneDrive\\桌面\\上海市临港地区人才租房补贴申请表.docx");
        File file2 = new File("D:\\Win10Driver\\OneDrive\\桌面\\上海市临港地区人才租房补贴申请表222.docx");

        // 加载现有的Word文档
        FileInputStream fis = new FileInputStream(file);
        XWPFDocument document = new XWPFDocument(fis);
        fis.close();

        // 创建新的水印
        XWPFHeader header = document.createHeader(HeaderFooterType.DEFAULT);
        CTHdrFtr ctHeader = header._getHdrFtr();
        XWPFParagraph watermark = new XWPFParagraph(ctHeader.addNewP(), header);
        watermark.setAlignment(ParagraphAlignment.CENTER);

        // 写入新的Word文档
        FileOutputStream out = new FileOutputStream(file2);
        document.write(out);
        out.close();
        document.close();
    }
    @Test
    public  void xxxx() throws Exception {
        File file = new File("D:\\Win10Driver\\OneDrive\\桌面\\上海市临港地区人才租房补贴申请表.docx");
        File file2 = new File("D:\\Win10Driver\\OneDrive\\桌面\\上海市临港地区人才租房补贴申请表222.docx");

        //输入的docx文档
        InputStream in = new FileInputStream(file);
        XWPFDocument doc= new XWPFDocument(in);

        // the body content
        XWPFParagraph paragraph = doc.createParagraph();
        XWPFRun run=paragraph.createRun();
        run.setText("The Body:");

        // create header-footer
        XWPFHeaderFooterPolicy headerFooterPolicy = doc.getHeaderFooterPolicy();
        if (headerFooterPolicy == null) headerFooterPolicy = doc.createHeaderFooterPolicy();

        // 水印内容
        headerFooterPolicy.createWatermark("张蛟龙");

        // get the default header
        // Note: createWatermark also sets FIRST and EVEN headers
        // but this code does not updating those other headers
        XWPFHeader header = headerFooterPolicy.getHeader(XWPFHeaderFooterPolicy.DEFAULT);
        paragraph = header.getParagraphArray(0);

        // get com.microsoft.schemas.vml.CTShape where fill color and rotation is set
        org.apache.xmlbeans.XmlObject[] xmlobjects = paragraph.getCTP().getRArray(0).getPictArray(0).selectChildren(
                new javax.xml.namespace.QName("urn:schemas-microsoft-com:vml", "shape"));

        if (xmlobjects.length > 0) {
            com.microsoft.schemas.vml.CTShape ctshape = (com.microsoft.schemas.vml.CTShape)xmlobjects[0];
            // set fill color
            ctshape.setFillcolor("#d8d8d8");
            // set rotation
            ctshape.setStyle(ctshape.getStyle() + ";rotation:315");
            //System.out.println(ctshape);
        }
        //文件输出地址
        FileOutputStream out = new FileOutputStream(file2);
        System.out.println("水印添加成功!");
        doc.write(out);
        out.close();
        doc.close();
    }
}
